package com.ma.common.mq;

import jakarta.annotation.Resource;
import org.apache.logging.log4j.util.Strings;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Properties;

@Component
public class RedisMQTemplate extends RedisTemplate<String,Object> {

    private String version = Strings.EMPTY;
    public String getVersion(){
        if (version.isEmpty()){
            RedisConnection connection = RedisConnectionUtils.getConnection(getConnectionFactory());
            Properties properties = connection.info();
            version  = properties.getProperty("redis_version");
            RedisConnectionUtils.releaseConnection(connection,getConnectionFactory());
        }
        return version;
    }
    Boolean isSupportBatchPull(){
        String version = getVersion();
        String[] arr = version.split("\\.");
        if (arr.length<2){
            return false;
        }
        Integer firVersion = Integer.valueOf(arr[0]);
        Integer secVersion = Integer.valueOf(arr[1]);
        return firVersion>6 || (firVersion ==6 && secVersion >=2);
    }
}
